package handler

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"web-app/web-server/internal/context"
	"web-app/web-server/internal/dao/model"
	"web-app/web-server/internal/logic"
	"web-app/web-server/internal/types"
)

func Login(ctx *context.Context, resp http.ResponseWriter, req *http.Request) error {
	defer req.Body.Close()
	body, _ := ioutil.ReadAll(req.Body)
	user := &model.User{}
	json.Unmarshal(body, user)
	res, err := logic.NewUserLogic(ctx).Login(user.Username, user.Password)
	if err != nil {
		return err
	}
	types.ResultJson(resp, res)
	return nil
}
